package com.liming.admin.controller;

import com.liming.admin.service.OrderService;
import com.liming.common.pojo.dto.order.admin.OrderPageQuery;
import com.liming.common.pojo.dto.order.admin.RefundPageQuery;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.order.OrderVO;
import com.liming.common.pojo.vo.order.web.OrderDetailVO;
import com.liming.common.pojo.vo.order.web.RefundOrderVO;
import com.liming.common.result.LMResult;
import com.liming.order.client.AdminOrderClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author liming
 * @date 2025/2/28
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "订单接口-Web")
@RequestMapping("/admin/order")
public class OrderController {

    private final AdminOrderClient adminOrderClient;
    private final OrderService orderService;

    @Operation(
            summary = "分页 - 订单",
            responses = {
                    @ApiResponse(responseCode = "1", description = "成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = OrderVO.class))
                    })
            }
    )
    @GetMapping
    public LMResult<PageVO<OrderVO>> page(OrderPageQuery query) {
        return LMResult.success(adminOrderClient.page(query));
    }

    @Operation(
            summary = "详情 - 订单项",
            responses = {
                    @ApiResponse(responseCode = "1", description = "成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = OrderDetailVO.class))
                    })
            }
    )
    @GetMapping("detail/{id}")
    public LMResult<OrderDetailVO> detail(@PathVariable Long id) {
        return LMResult.success(orderService.orderDetail(id));
    }

    @Operation(summary = "分页 - 退款")
    @GetMapping("refund")
    public LMResult<PageVO<RefundOrderVO>> refund(RefundPageQuery query) {
        return LMResult.success(orderService.refundPage(query));
    }

    @Operation(summary = "发货|自提 - 订单")
    @PutMapping("deliverOrPickup/{id}")
    public LMResult<Boolean> deliverOrPickup(@PathVariable Long id) {
        return LMResult.success(adminOrderClient.deliverOrPickup(id));
    }

    @Operation(summary = "完成 - 订单")
    @PutMapping("complete/{id}")
    public LMResult<Boolean> complete(@PathVariable Long id) {
        return LMResult.success(adminOrderClient.complete(id));
    }

    @Operation(summary = "取消 - 订单")
    @PutMapping("cancel/{id}")
    public LMResult<Boolean> cancel(@PathVariable Long id) {
        return LMResult.success(adminOrderClient.cancel(id));
    }

    @Operation(summary = "同意退款 - 订单")
    @PutMapping("agreeRefund/{id}")
    public LMResult<Boolean> agreeRefund(@PathVariable("id") Long refundOrderId) {
        return LMResult.success(orderService.agreeRefund(refundOrderId));
    }

    @Operation(summary = "拒绝退款 - 订单")
    @PutMapping("refuseRefund/{id}")
    public LMResult<Boolean> refuseRefund(@PathVariable("id") Long refundOrderId, @RequestParam String reason) {
        return LMResult.success(orderService.refuseRefund(refundOrderId, reason));
    }

    @Operation(summary = "查询退款 - 订单")
    @PutMapping("queryRefund/{id}")
    public LMResult<Boolean> queryRefund(@PathVariable("id") Long refundOrderId) {
        return LMResult.success(orderService.queryRefund(refundOrderId));
    }
}
